package cn.edu.tjufe.llp.tin.controller.part04;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.PastOrPresent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.tjufe.llp.tin.common.CommonException;
import cn.edu.tjufe.llp.tin.dto.common.Rs;
import cn.edu.tjufe.llp.tin.dto.part04.BookDto;
import cn.edu.tjufe.llp.tin.service.part04.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "BookController", description = "用于调试前端axios的Controller")
// @CrossOrigin(origins = "http://localhost:3000", maxAge = 3600)
@Validated
@RestController
@RequestMapping("/part04/books")        // 注意字符串最后有斜杠和没斜杠的区别
public class BookController {
	
	@Autowired
	private BookService bookService;
	
	@Operation(summary = "根据书名，作者，出版社，出版日期查询图书", description = "接收参数，返回对象数组，用于调试axios")
	@GetMapping("")			// 注意空字符串和斜杠的区别
	public Rs<List<BookDto>> findAll(@RequestParam(required = false) String name,
			@RequestParam(required = false) String author, 
			@RequestParam(required = false) String publisher,
			@RequestParam(required = false) @PastOrPresent(message = "出版日期不能是未来的时间") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
			@RequestParam(required = false) @PastOrPresent(message = "出版日期不能是未来的时间") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
			) {
		
		List<BookDto> books = bookService.selectList(name, author, publisher, begin, end);

		return Rs.success(books);
	}

	@Operation(summary = "根据ID查询一册图书", description = "返回一个对象，用于调试axios")
	@GetMapping("/{id}")
	public Rs<BookDto> findById(@PathVariable Long id) {
		BookDto book = bookService.selectById(id);
		return Rs.success(book);
	}
	
	@Operation(summary = "添加一册图书，返回图书的ID", description = "返回ID，用于调试axios")
	@PostMapping("")
	public Rs<Long> addBook(@Valid @RequestBody BookDto book) {

		long id = bookService.insert(book);
		
		return Rs.success(id);
	}

	@Operation(summary = "修改一册图书，返回图书的ID", description = "返回ID，用于调试axios")
	@PutMapping("/{id}")
	public Rs<Long> updateBook(@PathVariable Long id, @Valid @RequestBody BookDto book) {
		bookService.update(book, id);
		return Rs.success(id);
	}
	
	@Operation(summary = "删除一批图书，返回删除图书的数量", description = "返回删除图书的数量，用于调试axios")
	@DeleteMapping("/{ids}")
	public Rs<Long> deleteByIds(
			@Parameter(description = "id组成的数组.", required = true, example = "1,2,3") @PathVariable Long[] ids) {

		bookService.deleteByIds(ids);
		
		return Rs.success(0L + ids.length);
	}


	@Operation(summary = "返回失败消息", description = "返回失败消息，用于调试axios")
	@GetMapping("fail")
	public Rs<BookDto> fail() {
		return Rs.fail();
	}

	@Operation(summary = "抛出异常", description = "抛出异常，用于调试axios")
	@GetMapping("failException")
	public Rs<BookDto> failException() {
		throw new CommonException("出错了!");
	}
}
